package com.da.tools;

import com.da.result.ChatResult;
import com.da.result.MessageResult;
import com.da.result.ModelInfoResult;
import com.da.result.Result;
import com.da.result.model.Details;
import com.da.result.model.ModelInfo;
import com.da.tools.json.JsonMapperUtil;
import com.da.tools.json.JsonObject;
import com.da.tools.json.JsonParser;

import java.util.List;
import java.util.Map;

/**
 * 解析工具类
 *
 * @author da
 * @time 2024/7/4 下午 5:12
 */
public class Parse {
    // 解析json字符串的工具类
    private static final JsonParser jsonParser = new JsonParser();

    /**
     * 将Map转换为Json字符串
     *
     * @param map Map<K,V>
     * @param <K> K
     * @param <V> V
     * @return String
     */
    public static <K, V> String toJson(Map<K, V> map) {
        if (null == map || map.isEmpty()) return "{}";
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        map.forEach((k, v) -> {
            //添加键
            sb.append("\"").append(k).append("\":");
            //添加值
            if (v instanceof String) {
                String s = ((String) v).replaceAll("\n", "\\\\n");
                sb.append("\"").append(s).append("\",");
            } else {
                sb.append(v).append(",");
            }
        });
        sb.deleteCharAt(sb.length() - 1);
        sb.append("}");
        return sb.toString();
    }

    /**
     * 解析generate接口响应
     * {
     * "model":"qwen2:0.5b",
     * "created_at":"2024-07-08T01:51:00.2878382Z",
     * "response":"模型响应内容",
     * "done":true,
     * "done_reason":"stop",
     * "context":[这里面是Long类型的数值],
     * "total_duration":2663667500,
     * "load_duration":6288400,
     * "prompt_eval_count":8,
     * "prompt_eval_duration":263175000,
     * "eval_count":40,
     * "eval_duration":2390886000
     * }
     *
     * @param json 要解析的json
     * @return 转好的类型
     */
    public static Result fromGenerateJson(String json) {
        return JsonMapperUtil.fromJson(json, MessageResult.class);
    }

    /**
     * 解析chat接口响应
     * {
     * "model":"qwen2:0.5b",
     * "created_at":"2024-07-08T01:48:09.1228017Z",
     * "message":{"role":"assistant","content":"模型响应内容"},
     * "done_reason":"stop",
     * "done":true,
     * "total_duration":3780858100,
     * "load_duration":5108200,
     * "prompt_eval_duration":162519000,
     * "eval_count":60,
     * "eval_duration":3607773000
     * }
     *
     * @param json 要解析的json
     * @return 转好的类型
     */
    public static ChatResult fromChatJson(String json) {
        return JsonMapperUtil.fromJson(json, ChatResult.class);
    }

    /**
     * 解析tags 接口响应
     * {
     * "models": [
     * {
     * "name": "qwen2:0.5b",
     * "model": "qwen2:0.5b",
     * "modified_at": "2024-06-12T14:57:59.6948626+08:00",
     * "size": 352164041,
     * "digest": "6f48b936a09f7743c7dd30e72fdb14cba296bc5861902e4d0c387e8fb5050b39",
     * "details": {
     * "parent_model": "",
     * "format": "gguf",
     * "family": "qwen2",
     * "families": ["qwen2"],
     * "parameter_size": "494.03M",
     * "quantization_level": "Q4_0"}
     * },
     * { ... }
     * ]
     * }
     *
     * @param json tags 接口响应
     * @return 转好的类型
     */
    public static Result fromTagsJson(String json) {
        return JsonMapperUtil.fromJson(json, ModelInfoResult.class);
    }
}

